% SPLIT TRIALS INTO CYCLES 

s = 1;
n = 1;

for t = 1:numel(files)
    
    for c = 1:3
        
        if trialsummary(1,t).stimulus.smooth == 0  % Noisy
            
            Noisy(n).trial      = t;
            Noisy(n).cycle      = c;
            Noisy(n).no         = (t-1)*3 + c;
            Noisy(n).X(:,1)     = trialsummary(1,t).data.X...
                ((msPerCycle*(c-1)+1):(msPerCycle*c),1);
            Noisy(n).Y(:,1)     = trialsummary(1,t).data.Y...
                ((msPerCycle*(c-1)+1):(msPerCycle*c),1);
            Noisy(n).P(:,1)     = trialsummary(1,t).data.P...
                ((msPerCycle*(c-1)+1):(msPerCycle*c),1);
            try
            Noisy(n).sac        = trialsummary(1,t).data.sac...
                ((msPerCycle*(c-1)+1):(msPerCycle*c),1);
            end
            Noisy(n).time(:,1)  = 1:msPerCycle;
            
            n = n+1;
                
        else  % Smooth
            
            Smooth(s).trial      = t;
            Smooth(s).cycle      = c;
            Smooth(s).no         = (t-1)*3 + c;
            Smooth(s).X(:,1)     = trialsummary(1,t).data.X...
                ((msPerCycle*(c-1)+1):(msPerCycle*c),1);
            Smooth(s).Y(:,1)     = trialsummary(1,t).data.Y...
                ((msPerCycle*(c-1)+1):(msPerCycle*c),1);
            Smooth(s).P(:,1)     = trialsummary(1,t).data.P...
                ((msPerCycle*(c-1)+1):(msPerCycle*c),1);
            try
            Smooth(s).sac        = trialsummary(1,t).data.sac...
                ((msPerCycle*(c-1)+1):(msPerCycle*c),1);
            end
            Smooth(s).time(:,1)  = 1:msPerCycle;
            
            s = s+1;
        end
    end
end

total_noisy  = n-1;
total_smooth = s-1;
total_cycles = s + n;
clear s
clear n

% PLOT NOISY AND SMOOTH DATA ON SEPARATE CYCLE GRAPHS

figure(1)

for n = 1:total_noisy
    switch Noisy(n).cycle
        case 1
            subplot(3,3,1);
            np = plot(Noisy(n).X); hold on
        case 2
            subplot(3,3,2);
            np = plot(Noisy(n).X); hold on
        case 3
            subplot(3,3,3);
            np = plot(Noisy(n).X); hold on
    end
end

for s = 1:total_smooth
    switch Smooth(s).cycle
        case 1
            subplot(3,3,1);
            sp = plot(Smooth(s).X,'-r'); hold on
        case 2
            subplot(3,3,2);
            sp = plot(Smooth(s).X,'-r'); hold on
        case 3
            subplot(3,3,3);
            sp = plot(Smooth(s).X,'-r'); hold on
    end
end


% PLOT TARGET TRACE & OCCLUDERS

figure(1)
for c = 1:3
    subplot(3,3,c);
    ylim([-800 800]);
    xlabel('Time (ms)');
    ylabel('Displacement (pixels)');
    legend([sp,np],'22 deg/s Smooth','22 deg/s Noisy','Location','SouthEast')
%     legend boxoff
    title(['All trials at 22 deg/s, cycle ' num2str(c)],'FontWeight','bold')
    plot(target22.one,'-k','LineWidth',2)
    
    for i = 1:2
        patch(target22.plotocc.x(i,:),target22.plotocc.y(i,:),[0.9,0.9,0.9]);
        set(patch,'facealpha',0.1)
    end
end

try
    suplabel(subject,'t')
catch
    disp('no suplabel function')
end
